package com.godot.service;

import javax.jdo.Transaction;

import com.godot.entity.manager.GodotManger;
import com.godot.entity.persistent.Godot;
import com.godot.util.Log;
import com.godot.util.UserUtil;

public class AccountService extends Service {
	
	private static final int kActionEditAccountInfo = 1;
	
	@Override
	protected void doExcute() {
		super.doExcute();
	}
	
	@Override
	protected void dispatch() {
		super.dispatch();
		switch (_action) {
		case kActionEditAccountInfo:
			this.editAccountInfo();
			break;
		default:
			break;
		}
	}

	private void editAccountInfo() {
		Log.info("edit account info !");
		String key = (String) _session.getAttribute("key");
//		Godot godot = new GodotManger(_pm).getGodotById(key);
		String pwd = _req.getParameter("pwd1");
		String email = _req.getParameter("email");
		String qName = _req.getParameter("qName");
		Transaction tx = _pm.currentTransaction();
		try {
			tx.begin();
			Godot godot = new GodotManger(_pm).getGodotById(key);
			if (UserUtil.checkPwd(pwd)) {
				godot.setPassword(pwd);
			}
			if (UserUtil.checkEmail(email)) {
				godot.setEmail(email);
			}
			if (UserUtil.checkQName(qName)) {
				godot.setQName(qName);
			}
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

}
